﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Collections.ObjectModel;

using System.IO;
using System.Xml.Serialization;
using System.Xml;

namespace RZD_maps.Modules.Map.Model
{
    class TransportUtil
    {

        public static ObservableCollection<BaseCategory> CreatePath()
        {
            ObservableCollection<BaseCategory> list = new ObservableCollection<BaseCategory>();
            if (File.Exists("data.xml"))
            {
                DeserializeObject(list, "data.xml");
            }
            return list;
        }



        public static void DeserializeObject(ObservableCollection<BaseCategory> item, string FilePath)
        {
            double lat_prev=0.0;
            double lng_prev = 0.0;
//            int dt = 0;
            XmlDocument xml = new XmlDocument();
            try
            {
                xml.Load(FilePath);
                XmlElement root = xml.DocumentElement;
                item.Clear();
                //foreach (BaseCategory dl in item)
                //{
                //    item.SubCategories.Remove(item);
                //};
                bool bFirst = true;
                foreach (XmlElement el in root.ChildNodes)
                {
                    string rtName = el.GetAttribute("name");
                    BaseCategory route = new BaseCategory(rtName);
                    item.Add(route);
                    foreach (XmlElement it in el.ChildNodes)
                    {
                        string ptName = it.GetAttribute("name");
                        string ptLng = it.GetAttribute("lng");
                        string ptLat = it.GetAttribute("lat");
                        string ptVelocity = it.GetAttribute("velocity");

                        BaseCategory cat = new BaseCategory(ptName, route);
                        double val = 0.0;
                        Double.TryParse(ptLng, out val);
                        cat.Lng = val;
                        
                        val = 0.0;
                        Double.TryParse(ptLat, out val);
                        cat.Lat = val;

                        val = 0.0;
                        Double.TryParse(ptVelocity, out val);
                        cat.Velocity = val;
                        if (bFirst == true)
                        {
                            bFirst = false;
                            lat_prev = cat.Lat;
                            lng_prev = cat.Lng;
                        }
                        else
                        {
                            if ((lat_prev == cat.Lat) || (lng_prev == cat.Lng))
                                continue;
                        }
//                        dt++;
//                        if (dt < 10) continue;
//                        dt = 0;
                        route.SubCategories.Add(cat);
                        cat.SubCategories.Add(new BaseCategory(string.Format("Долгота: {0}", cat.Lng), cat));
                        cat.SubCategories.Add(new BaseCategory(string.Format("Широта: {0}", cat.Lat), cat));
                        cat.SubCategories.Add(new BaseCategory(string.Format("{1}: Скорость : {0}", cat.Velocity, rtName), cat));
                    }
                }
            }
            catch (Exception ex)
            {
                int t = 0;
            }
        }

        }
}
